<?php
	include_once("./dal/TopicDAL.php");
	include_once("./dal/PostDAL.php");
	
	$newTopicName = "";
	$newTopicDescription = "";
	
	$newPostTopicID = "";
	$newPostTitle = "";
	$newPostBody = "";
	
	//new topic submitted
	$newTopicFormIsValid = false;
	$updateTopicFormIsValid = false;
	$topicUpdateAttempted = false;
	
	$newPostFormIsValid = false;
	$updatePostFormIsValid = false;
	$postUpdateAttempted = false;
	
	if(isset($_POST["btnCreateTopic"]))
	{	
		if($_POST["btnCreateTopic"]=="Create Topic")
		{		
	
			if(isset($_POST["txtNewTopicName"]))
			{
				$newTopicName = trim($_POST["txtNewTopicName"]);
			}

			if(isset($_POST["txtNewTopicDescription"]))
			{
				$newTopicDescription = trim($_POST["txtNewTopicDescription"]);
			}			
			
			$newTopicFormIsValid = true;
			
			if($newTopicFormIsValid)
			{
				$result = createTopic($newTopicName, $newTopicDescription, $username);
				
				$newTopicSuccess = $result;
				
				if($newTopicSuccess)
				{
					header(sprintf('Location: community.php?action=newtopic&topic=%d&success',$result));
					
					exit;					
				}
			}
		}
	}
	if(isset($_POST["btnCreatePost"]))
	{	
		if($_POST["btnCreatePost"]=="Post")
		{
			if(isset($_POST["txtNewPostTopicID"]))
			{
				$newPostTopicID = trim($_POST["txtNewPostTopicID"]);
			}
			
			if(isset($_POST["txtNewPostTitle"]))
			{
				$newPostTitle = trim($_POST["txtNewPostTitle"]);
			}

			if(isset($_POST["txtNewPostBody"]))
			{
				$newPostBody = trim($_POST["txtNewPostBody"]);
			}			
			
			$newPostFormIsValid = true;
			
			if($newPostFormIsValid)
			{
				$result = createPost($newPostTopicID, $newPostTitle, $newPostBody, $username);
				
				$newPostSuccess = $result;
				
				if($newPostSuccess)
				{
					header(sprintf('Location: community.php?action=newpost&topic=%d&post=%d&success',$newPostTopicID,$result));
					exit;					
				}
			}
		}
	}
	if(isset($_POST["btnUpdateTopic"]))
	{	
		$topicName = "";
		$topicDescription = "";
		$topicID = "";
		
		if($_POST["btnUpdateTopic"]=="Update")
		{
			$topicUpdateAttempted = true;
			
			if(isset($_POST["txtUpdateTopicID"]))
			{
				$topicID= trim($_POST["txtUpdateTopicID"]);
			}
			if(isset($_POST["txtUpdateTopicName"]))
			{
				$topicName = trim($_POST["txtUpdateTopicName"]);
			}
			if(isset($_POST["txtUpdateTopicDescription"]))
			{
				$topicDescription = trim($_POST["txtUpdateTopicDescription"]);
			}	

			$updateTopicFormIsValid = true;
			
			if($updateTopicFormIsValid)
			{
				$result = updateTopic($topicID, $topicName, $topicDescription);
				
				$updateTopicSuccess = $result;
				
				if($updateTopicSuccess)
				{
					header(sprintf('Location: community.php?action=edittopic&topic=%d&success',$topicID));
					exit;					
				}
				else
				{
					header(sprintf('Location: community.php?action=edittopic&topic=%d&failure',$topicID));
					exit;
				}
			}			
		}
	}
	if(isset($_POST["btnUpdatePost"]))
	{	
		$postTitle = "";
		$postBody = "";
		$postID = "";
		
		if($_POST["btnUpdatePost"]=="Update")
		{
			$postUpdateAttempted = true;
			
			if(isset($_POST["txtUpdatePostID"]))
			{
				$postID= trim($_POST["txtUpdatePostID"]);
			}
			if(isset($_POST["txtUpdatePostTitle"]))
			{
				$postTitle = trim($_POST["txtUpdatePostTitle"]);
			}
			if(isset($_POST["txtUpdatePostBody"]))
			{
				$postBody = trim($_POST["txtUpdatePostBody"]);
			}	

			$updatePostFormIsValid = true;
			
			if($updatePostFormIsValid)
			{
				$result = updatePost($postID, $postTitle, $postBody);
				
				$updatePostSuccess = $result;
				
				if($updatePostSuccess)
				{
					header(sprintf('Location: community.php?action=editpost&post=%d&success',$postID));
					exit;					
				}
				else
				{
					header(sprintf('Location: community.php?action=editpost&post=%d&failure',$postID));
					exit;
				}
			}			
		}
	}
	else
	{
		if(isset($_REQUEST["action"]))
		{
			if(isset($_REQUEST["topic"]))
			{
				$topicID = $_REQUEST["topic"];
				
				if(isset($_REQUEST["post"]))
				{
					$postID = $_REQUEST["post"];
					
					switch(strtolower($_REQUEST["action"]))
					{
						case 'removepost':	
							$result = removePost($topicID, $postID);
						break;
					}					
				}
				else
				{
					switch(strtolower($_REQUEST["action"]))
					{
						case 'removetopic':	
							$result = removeTopic($topicID);
						break;
					}
				}
			}
		}
	}
	
	function getContent()
	{
		if(isset($_REQUEST["action"]))
		{
			switch(strtolower($_REQUEST["action"]))
			{
				case 'newtopic':
					if(isLoggedIn())
					{
						if(checkUserRole("Registered User") || checkUserRole("Administrator"))
						{
							if(isset($_REQUEST["success"]) && isset($_REQUEST["topic"]))
							{
								$newTopicID = $_REQUEST["topic"];
								include_once('./templates/CreateTopicSuccess.php');
							}
							else
							{
								include_once('./templates/NewTopicForm.php'); 	
							}
						}
					}
					else
					{
						header('Location: login.php');
					}
				break;
				
				case 'newpost':
					if(isLoggedIn())
					{
						if(checkUserRole("Registered User") || checkUserRole("Administrator"))
						{
							$postCreatedSuccess = false;
							if(isset($_REQUEST["success"]) && isset($_REQUEST["post"]))
							{
								$postCreatedSuccess = true;
							}
							$topicID = $_REQUEST["topic"];
							$defaultPosts = getTopicPosts($topicID);
							$currentTopic = getTopic($topicID);
							include_once('./templates/DefaultTopicContent.php');				
						}
					}
					else
					{
						header('Location: login.php');
					}
				break;
				
				case 'removetopic':
					if(isLoggedIn())
					{	
						if(checkUserRole("Administrator"))
						{
							//no custom content is required to be displayed
						}
					}
				break;
				
				case 'removepost':
					if(isLoggedIn())
					{	
						if(checkUserRole("Administrator"))
						{
							//no custom content is required to be displayed
						}
					}
				break;
				
				case 'edittopic':
					global $topicUpdateAttempted;
					
					if(isLoggedIn())
					{
						if(checkUserRole("Administrator"))
						{							
							if(isset($_REQUEST["success"]) && isset($_REQUEST["topic"]))
							{
								$successTopicID = $_REQUEST["topic"];
								include_once('./templates/UpdateTopicSuccess.php');
							}
							else if(isset($_REQUEST["failure"]) && isset($_REQUEST["topic"]))
							{
								$failureTopicID = $_REQUEST["topic"];
								include_once('./templates/UpdateTopicFailure.php');								
							}
							else if(isset($_REQUEST["topic"]))
							{
								$topicID = $_REQUEST["topic"];
								$editableTopic = getTopic($topicID);
								$updateTopicID = $editableTopic["topic_id"];
								$updateTopicName = $editableTopic["name"];
								$updateTopicDescription = $editableTopic["description"];;
								include_once('./templates/EditTopicForm.php');
							}
						}
					}
				break;
				default:
					//invalid action
				break;
			}
		}
		else if(isset($_REQUEST["topic"]))
		{
			$topicID = $_REQUEST["topic"];
			$defaultPosts = getTopicPosts($topicID);
			$currentTopic = getTopic($topicID);
			include_once('./templates/DefaultTopicContent.php');
		}
		else
		{
			$defaultTopicList = getAllTopics();
			include_once('./templates/DefaultCommunityContent.php'); 
		}
	}
?>